/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MySqlAccess;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

/**
 *
 * @author LNThanh
 */
public final class DataAcessHelper {

    private static String m_server;
    private static String m_user;
    private static String m_pass;
    private static int    m_port;
    private static String m_databaseName;
    private static Connection m_conn;
    
    private static DataAcessHelper m_instance = new DataAcessHelper("localhost", "baonhattin", "root", "123", 3306);

    public static Connection GetConnection()
    {
        return m_conn;
    }

    private DataAcessHelper()
    {
        m_server = m_user = m_pass = m_databaseName = "";
    }
    
    public static void SetDatabaseName(String databaseName)
    {
        m_databaseName = databaseName;
    }
    
    public static DataAcessHelper GetInstance()
    {
        return m_instance;
    }    
    
    
    @Override
    public void finalize() throws Throwable
    {
        if (m_conn != null) m_conn.close();
        
        super.finalize();        
    }
    public static void SetServer(String name)
    {
        m_server = name;
    }
    
    public static void SetUser(String user)
    {
        m_user = user;
    }
    
    public static void SetPass(String pass)
    {
        m_pass = pass;
    }
    
    private DataAcessHelper(String server, String databaseName, String user, String pass, int port)
    {
        m_server = server;
        m_user = user;
        m_pass = pass;
        m_port = port;
        m_databaseName = databaseName;
        
        CreateConnection();
    }
    
    public static void CreateConnection()
    {        
        try
        {
            if (m_conn != null)
            {
                m_conn.close();
            }
            com.mysql.jdbc.jdbc2.optional.MysqlDataSource ds  = new com.mysql.jdbc.jdbc2.optional.MysqlDataSource();
            ds.setServerName(m_server);
            ds.setPortNumber(m_port);
            ds.setDatabaseName(m_databaseName);
            ds.setUser(m_user);
            ds.setPassword(m_pass);
            ds.setEncoding("utf-8");
            m_conn = ds.getConnection();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
    
    public static ResultSet ExcuteQuery(String query)
    {
        try
        {
            java.sql.Statement statement = m_conn.createStatement();
            if (statement.execute(query))
            {
                return statement.getResultSet();
            }
            else
            {
                System.out.println("Cannot excute statement: " + query);
            }
                
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        
        return null;
    }
    
    public static void main(String[] args) 
    {
        try 
        {
            
        } catch (Exception e) {
          e.printStackTrace();
        }
    }
}
